﻿using System;
using System.Collections.Generic;
using System.Text;
using Model;

namespace BLL
{
    /// <summary>
    /// 实现 购物车 / 临时数据表 的对象.
    /// </summary>
    [Serializable]
    public class Cart
    {
        // Internal storage for a cart or items
        private Dictionary<string, CardItems> cartItems = new Dictionary<string, CardItems>();
 
        /// <summary>
        /// 统计总金额
        /// </summary>
        public decimal TotalCost
        {
            get
            {
                decimal total = 0;
                foreach (CardItems item in cartItems.Values)
                    total += item.Price * item.Quantity;
                return total;
            }
        }

        /// <summary>
        /// 统计总数量
        /// </summary>
        public int TotalQty
        {
            get
            {
                int qty = 0;
                foreach (CardItems item in cartItems.Values)
                    qty += item.Quantity;
                return qty;
            }
        }
        public string BuildItemID
        {
            get
            {
                int itemid = 0;
                foreach (CardItems item in cartItems.Values)
                {
                    if (itemid < int.Parse(item.ItemId))
                        itemid = int.Parse(item.ItemId);
                }
                itemid++;
                return itemid.ToString();
            }
        }
        /// <summary>
        /// 更新 设备/商品 的数量
        /// </summary>
        /// <param name="itemId">Item Id</param>
        /// <param name="qty">Quantity</param>
        public void SetQuantity(string itemId, int qty)
        {
            cartItems[itemId].Quantity = qty;
        }


        /// <summary>
        /// 更新 设备/商品 数量、折扣、单价
        /// </summary>
        public void SetItems(string itemId, int qty, decimal price)
        {
            if (qty != -1)
                cartItems[itemId].Quantity = qty;

            if (price != -1)
                cartItems[itemId].Price = price;
        }
  
        /// <summary>
        /// 数据记录的行数
        /// </summary>
        public int Count
        {
            get { return cartItems.Count; }
        }

        /// <summary>
        /// 新增 设备/商品 至购物车 / 单据设备清单
        /// 当设备/商品已存在时,只实现数量的增加
        /// </summary>
        /// <param name="itemId">Item Id of item to add</param>
        public void Add(string itemId)
        {
            CardItems cartItem;
            if (!cartItems.TryGetValue(itemId, out cartItem))
            {
                //Item item = new Item();
                Model.ProductBase item = BLL.ProductBase.GetProductObjById(itemId);
                if (item != null)
                {
                    CardItems newItem = new CardItems(itemId, item.ProductName, 1, item.Price, item.ProductId, item.ProductName);
                    cartItems.Add(itemId, newItem);
                }
            }
        }

        /// <summary>
        /// 新增 设备/商品 至购物车 / 单据设备清单
        /// 当设备/商品已存在时,只实现数量的增加
        /// </summary>
        /// <param name="itemId">Item Id of item to add</param>
        public void Add(string itemId, int _qty)
        {
            CardItems cartItem;
            if (!cartItems.TryGetValue(itemId, out cartItem))
            {
                //Item item = new Item();
                Model.ProductBase item = BLL.ProductBase.GetProductObjById(itemId);
                if (item != null)
                {
                    CardItems newItem = new CardItems(itemId, item.ProductName, _qty, item.Price, item.ProductId, item.ProductName);
                    cartItems.Add(itemId, newItem);
                }
            }
            else
            {
                if (_qty > 1)
                    cartItem.Quantity = _qty;
                else
                    cartItem.Quantity++;
            }
        }

        /// <summary>
        /// 新增 设备/商品 至购物车 / 单据设备清单
        /// 当设备/商品已存在时,只实现数量的增加
        /// </summary>
        /// <param name="itemId">Item Id of item to add</param>
        public void Add(string itemId, string orderId, int _qty, decimal _price)
        {
            CardItems cartItem;
            if (!cartItems.TryGetValue(itemId, out cartItem))
            {
                //Item item = new Item();
                Model.ProductBase item = BLL.ProductBase.GetProductObjById(itemId.Split('|')[0]);
                if (item != null)
                {
                    CardItems newItem = new CardItems(itemId, item.ProductName, _qty, _price, item.ProductId, item.ProductName, orderId);
                    cartItems.Add(itemId, newItem);
                }
            }
        }

        /// <summary>
        /// 新增 设备/商品 至购物车 / 单据设备清单
        /// 当设备/商品已存在时,只实现数量的增加
        /// </summary>
        /// <param name="item">Item to add</param>
        public void Add(CardItems item)
        {
            CardItems cartItem;
            if (!cartItems.TryGetValue(item.ItemId, out cartItem))
                cartItems.Add(item.ItemId, item);
            else
                cartItem.Quantity += item.Quantity;
        }

        /// <summary>
        /// 配备 itemId 从清单移出 设备/商品
        /// </summary>
        /// <param name="itemId">ItemId of item to remove</param>
        public void Remove(string itemId)
        {
            cartItems.Remove(itemId);
        }

        /// <summary>
        /// 购物车 / 临时数据表的 数据清单
        /// </summary>
        /// <returns>Collection of CartItemInfo</returns>
        public ICollection<CardItems> CartItems
        {
            get { return cartItems.Values; }
        }

        /// <summary>
        /// 清除清单中的所有记录
        /// </summary>
        public void Clear()
        {
            cartItems.Clear();
        }

        /// <summary>
        /// 清除清单中的所有记录
        /// </summary>
        public void Clear(string orderId)
        {
            foreach (Model.CardItems item in cartItems.Values)
            {
                if (item.OrderId == orderId)
                    cartItems.Remove(item.ItemId);
            }
        }

        /// <summary>
        /// Method to convert all cart items to order line items
        /// </summary>
        /// <returns>A new array of order line items</returns>
        public Model.OrderItems[] GetOrderItems()
        {
            Model.OrderItems[] orderLineItems = new OrderItems[cartItems.Count];
            int lineNum = 0;

            foreach (Model.CardItems item in cartItems.Values)
                orderLineItems[lineNum] = new OrderItems("",  ++lineNum, item.Quantity, item.Price, item.Name,item.ProductId);

            return orderLineItems;
        }

        /// <summary>
        /// Method to convert all cart items to order line items
        /// </summary>
        /// <returns>A new array of order line items</returns>
        public Model.OrderItems[] GetOrderItems(string orderId)
        {
            int lineNum = 0;
            IList<Model.OrderItems> items = new List<Model.OrderItems>(); 
            foreach (Model.CardItems item in cartItems.Values)
            {
                if (item.OrderId == orderId)
                    items.Add(new OrderItems(orderId, ++lineNum, item.Quantity, item.Price, item.Name, item.ProductId));
            }

            Model.OrderItems[] orderLineItems = new OrderItems[items.Count];

            items.CopyTo(orderLineItems, 0);

            return orderLineItems;
        }
    }
}
